<?php
/* OpenBIBLIO -- A library administration web interface
 * Copyright (C) 2011-2012 Laurent Luyssen   <big.lol13@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

include_once "includes/searchDAO.inc";
include_once "includes/colCfgDAO.inc";

function _displayColumns($title, $page = "search") {
	// Defined in admin.php
	global $action;
	global $type;
	
	$allowedCols = SearchDAO::getAllowedCols();
	$col_cfg = new ColCfgDAO($_SESSION["login"], $page);
	$cols = $col_cfg->getConfig();
	// If any id is -1, this is a default config -> initialize it
	if ($cols[0]->id == -1)
		$col_cfg->setConfig ();
	echo "<center><h3>$title</h3>";
?>
<table cellspacing="7">
	<tr>
		<th><?php echo _("Colonne"); ?></th>
		<th colspan="3"><?php echo _("Action"); ?></th>
	</tr>
<?php
	$link = "admin.php?action=$action&type=$type&page=".urlencode($page);
	$nb_cols = count($cols);
	for ($i = 0; $i < $nb_cols; $i++) {
		echo "<tr>";
		echo "<td>".$allowedCols[$cols[$i]->db_name]."</td>";
		echo "<td>";
		$id = $cols[$i]->id;
		if ($i > 0) {
			echo '<a href="'.$link.'&action2=up&id='.$id.'">';
			echo '<img src="images/b_plus.png" title="'._("Monter").'"/></a>&nbsp;';
		}
		echo "</td><td>";
		if ($i != $nb_cols - 1) {
			echo '<a href="'.$link.'&action2=down&id='.$id.'">';
			echo '<img src="images/b_minus.png" title="'._("Descendre").'"/></a>&nbsp;';
		}
		echo "</td><td>";
		echo '<a href="'.$link.'&action2=del&id='.$id.'">';
		echo '<img src="images/b_delete.png" title="'._("Supprimer").'"/></a>&nbsp;';
		echo "</td>";
		echo "</tr>";
	}
	$select  = '<select name="col_db_name">';
	foreach ($allowedCols as $db_name => $name) {
		if (!empty($name))
			$select .= '<option value="'.$db_name.'">'.$name.'</option>';
	}
	$select .= '</select>';
	echo '<tr><form method="GET">';
	echo '<input type="hidden" name="action" value="'.$action.'">';
	echo '<input type="hidden" name="type" value="'.$type.'">';
	echo '<input type="hidden" name="page" value="'.urlencode($page).'">';
	echo '<input type="hidden" name="action2" value="add">';	
	echo "<td>$select</td>\n";
	echo '<td colspan="3"><input type="submit" value="'._("Ajouter").'" /></td>';
	echo "</form></tr>\n";
	echo "</table></center>";
}

if (isset($_GET['page']) && (!empty($_GET['page']))) {
	$col_cfg = new ColCfgDAO($_SESSION["login"], $_GET['page']);
	$col_cfg->getConfig();
	if (isset($_GET['action2']) ) {
		switch ($_GET['action2']) {
			case 'up':
				$col_cfg->moveUp($_GET['id']);
				$col_cfg->setConfig();
				break;
			case 'down':
				$col_cfg->moveDown($_GET['id']);
				$col_cfg->setConfig();
				break;
			case 'del':
				$col_cfg->delete($_GET['id']);
				$col_cfg->setConfig();
				break;
			case 'add':
				$col = new ColCfg(99, $_GET['col_db_name']);
				if ($col_cfg->add($col))
					$col_cfg->setConfig();
				break;
		}
	}
}
?>
	
<center>
<table cellspacing ="15">
	<tr>
		<td valign="top"><?php _displayColumns(_("Recherche"), "search"); ?></td>
		<td valign="top"><?php _displayColumns(_("Export"), "export"); ?></td>
		<?php
		$lusDir = getBaseDir()."/contrib/lus/";
		if (is_dir($lusDir)) {
			echo '<td valign="top">';
			_displayColumns(_("Livres lus"), "read");
			echo '</td>';
		}
		?>
	</tr>
</table>
</center>	
<br><br>
<div>
	<span id=left-text>
		<A HREF="admin.php?menu=1"><?php echo _("retour menu administration"); ?></A><BR>
		<A HREF="index.php"><?php echo _("retour menu principal"); ?></A>
	</span>
</div>
<br>